package com.spring.cootask.web.service.impl;

import com.spring.cootask.web.dao.FeTaskTablebaseDetailSysMapper;
import com.spring.cootask.web.model.FeTaskTablebaseDetailSys;
import com.spring.cootask.web.service.SysColumnService;
import com.spring.security.web.model.BaseResult;
import com.spring.tools.MyTools;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.util.List;

/**
 * Created by WoJunyu on 2016/10/8.
 */
@Service
public class SysColumnServiceImpl implements SysColumnService{

    @Resource private FeTaskTablebaseDetailSysMapper feTaskTablebaseDetailSysMapper;

    @Override
    public BaseResult deleteByPrimaryKey(String id) {
        BaseResult ret = new BaseResult();
        int i = feTaskTablebaseDetailSysMapper.deleteByPrimaryKey(id);
        if(i==0){
            ret.setStatus("0");
            ret.setMsg("删除失败");
        }
        return ret;
    }

    @Override
    public BaseResult insert(FeTaskTablebaseDetailSys record) {
        BaseResult ret = new BaseResult();
        int i = feTaskTablebaseDetailSysMapper.insert(record);
        if(i==0){
            ret.setStatus("0");
            ret.setMsg("保存失败");
        }
        return ret;
    }

    @Override
    public FeTaskTablebaseDetailSys selectByPrimaryKey(String id) {
        return feTaskTablebaseDetailSysMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<FeTaskTablebaseDetailSys> selectByPrimaryParm(FeTaskTablebaseDetailSys parm) {
        return feTaskTablebaseDetailSysMapper.selectByPrimaryParm(parm);
    }
    
    @Override
    public BaseResult saveOrUpdateSysColumn(FeTaskTablebaseDetailSys feTaskTablebaseDetailSys) throws Exception {
    	int effect=0;
    	
    	if(feTaskTablebaseDetailSys.getParentid()==null){
    		feTaskTablebaseDetailSys.setType(-1);
    		feTaskTablebaseDetailSys.setItemType(-1);
    	}
    	
    	if(feTaskTablebaseDetailSys.getId().equals("0")){
    		effect=feTaskTablebaseDetailSysMapper.insert(feTaskTablebaseDetailSys);
    	}else{
    		effect=feTaskTablebaseDetailSysMapper.updateByPrimaryKeySelective(feTaskTablebaseDetailSys);
    	}
    	return MyTools.checkResult(effect);
    }

    @Override
    public BaseResult delSysColumn(String id) throws Exception{
    	
    	FeTaskTablebaseDetailSys feTaskTablebaseDetailSys=new FeTaskTablebaseDetailSys();    	
    	feTaskTablebaseDetailSys.setId(id);
    	
    	List<FeTaskTablebaseDetailSys> list=feTaskTablebaseDetailSysMapper.selectByPrimaryParm(feTaskTablebaseDetailSys);    	
    	delSysColumn(list);
    	return new BaseResult("1","删除成功");
    }
     //递归删除
    public void delSysColumn(List<FeTaskTablebaseDetailSys> list){
    	for(FeTaskTablebaseDetailSys feTaskTablebaseDetailSys :list){
    		
    		List<FeTaskTablebaseDetailSys> children = feTaskTablebaseDetailSys.getChildren();
    		
    		if(children==null||children.size()==0){
    			feTaskTablebaseDetailSysMapper.deleteByPrimaryKey(feTaskTablebaseDetailSys.getId());
    		}else{
    			delSysColumn(children);
    		}    		
    		    feTaskTablebaseDetailSysMapper.deleteByPrimaryKey(feTaskTablebaseDetailSys.getId());    		
    	}
    }

    @Override
    public BaseResult updateByPrimaryKeySelective(FeTaskTablebaseDetailSys record) {
        BaseResult ret = new BaseResult();
        int i = feTaskTablebaseDetailSysMapper.updateByPrimaryKeySelective(record);
        if(i==0){
            ret.setStatus("0");
            ret.setMsg("更新失败");
        }
        return ret;
    }
}
